package com.showcai.controller.user;

import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.AdminUser;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.AdminUserService;
import com.showcai.tools.RandomValidateCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Baggio on 2018-9-6.
 */
@RequestMapping("/login")
@Controller
public class LoginController extends BaseController {

    @Autowired
    AdminUserService adminUserService;

    /**
     * 获取验证码
     */
    @RequestMapping(value = "/getVerificatioCode", method = RequestMethod.GET)
    @ResponseBody
    public void getVerificationCode(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandomCode(request, response);// 输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/gotoLogin")
    public ModelAndView showAdvertList() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/user/login");
        return mov;
    }
    /**
     * 登录请求
     * param:userName,passWord,code
     */

    @RequestMapping(value = "/Login", method = RequestMethod.POST)
    @ResponseBody
    public String Login(AdminUser adminUser, String code) {
        //applicationContext.
        logger.info(adminUser.toString());
        logger.info(code);

        String str = (String) super.session.getAttribute(RandomValidateCode.RANDOMCODEKEY);
        ResultObj resultObj = new ResultObj();

        try {
            logger.info(code + "<=====================================>" + str);
            if (StringUtils.isBlank(code)) {
                throw new MyRunTimeExcption("图像验证码不能为空！！！");
            }
            if (!(str.toLowerCase().equals(code.toLowerCase()))) {
                throw new MyRunTimeExcption("验证码错误！");
            }

            adminUser = adminUserService.login(adminUser);
            logger.info(adminUser.getTid());
            logger.info(adminUser.getRealName());
            session.setAttribute(Constant.SESSION_KEY_ADMIN_USER_LOGIN, adminUser);
            resultObj.setMsg("登陆成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());

        } catch (Exception ex) {
            ex.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(ex.getMessage());
        }
        return resultObj.toJson();
    }


}
